package com.UnionPayCenter.controller;

import com.UnionPayCenter.entity.IntegralRuleEntity;
import com.UnionPayCenter.entity.ResponseWrapper;
import com.UnionPayCenter.entity.UserEntity;
import com.UnionPayCenter.entity.vo.UserVo;
import com.UnionPayCenter.enums.ErrorCodeEnum;
import com.UnionPayCenter.exception.ApiException;
import com.UnionPayCenter.jwt.CurrentManager;
import com.UnionPayCenter.jwt.CurrentShop;
import com.UnionPayCenter.service.IIntegralRuleService;
import com.UnionPayCenter.service.IUserService;
import com.UnionPayCenter.swagger.ApiJsonObject;
import com.UnionPayCenter.swagger.ApiJsonProperty;
import com.UnionPayCenter.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: UnionPayCenter
 * @description: 积分赠送规则
 * @author: bite
 * @create:
 **/
@Slf4j
@Api(value = "integralRule", tags = "积分赠送规则")
@RestController
@RequestMapping("/integralRule")
public class IntegralRuleController {
    @Autowired
    private IIntegralRuleService ruleService;

    @CurrentManager
    @CurrentShop
    @RequestMapping(value = "/findRegistry", method = RequestMethod.POST)
    @ApiOperation(tags = "积分赠送规则", httpMethod = "POST", value = "[后台]查询注册积分赠送规则", response = IntegralRuleEntity.class)
    public ResponseWrapper findRegistry(
            @ApiJsonObject(name = "integralRuleFindRegistryParams", value = {
                    @ApiJsonProperty(key = "token", value = "token值"),
                    @ApiJsonProperty(key = "shopId", value = "商铺id", type = "int")
            }) @RequestBody JSONObject jsonParam) {
        try {
            IntegralRuleEntity ruleEntity = ruleService.findRegistry(jsonParam);
            return ResponseWrapper.succeed(ruleEntity);
        }catch (ApiException e){
            log.error(String.format("[后台]查询注册积分赠送规则失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[后台]查询注册积分赠送规则失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    @CurrentManager
    @CurrentShop
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(tags = "积分赠送规则", httpMethod = "POST", value = "[后台]修改积分赠送规则", response = String.class)
    public ResponseWrapper update(
            @ApiJsonObject(name = "integralRuleUpdateParams", value = {
                    @ApiJsonProperty(key = "token", value = "token值"),
                    @ApiJsonProperty(key = "shopId", value = "商铺id", type = "int"),
                    @ApiJsonProperty(key = "ruleId", value = "规则id", type = "int"),
                    @ApiJsonProperty(key = "isGive", value = "是否赠送（0-是 1-否）", type = "int"),
                    @ApiJsonProperty(key = "number", value = "赠送积分数量", type = "int")
            }) @RequestBody JSONObject jsonParam) {
        try {
            Integer ruleId = jsonParam.getInteger("ruleId");
            if (ruleId == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "ruleId"));
            }
            Integer isGive = jsonParam.getInteger("isGive");
            if (isGive == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "isGive"));
            }
            Integer number = jsonParam.getInteger("number");
            if (number == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "number"));
            }
            ruleService.update(jsonParam);
            return ResponseWrapper.succeed("修改成功");
        }catch (ApiException e){
            log.error(String.format("[后台]修改积分赠送规则失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[后台]修改积分赠送规则失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }
}
